
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recordDate:'',
    startDate: '2020-01-01 00:00',
    curCate: '',
    imageAttr: [],
    fs_status: {},
    statusIndex: -1,
    showModal: false,
    modalType:1,
    yaoUnitIndex:0,
    yaoAttr: [],
    medicines: [],
    unitAttr1: [],
    unitAttr2: [],
    unitAttr3: [],
    curUnit:[0,0,0],
    description: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {


    this.setData({
      curCate: e.cateId
    })
    this.initData()
    this.getBaby()


    for (let i = 0; i < 50; i++) {
      this.data.unitAttr1.push(i)
    }
    this.setData({
      unitAttr1: this.data.unitAttr1
    })

    for (let i = 0; i < 10; i++) {
      this.data.unitAttr2.push('.'+i)
    }
    this.setData({
      unitAttr2: this.data.unitAttr2
    })
  },
  addMedicines(e) {
    this.setData({
      showModal: true,
      modalType:e.currentTarget.dataset.type
    })
    if(e.currentTarget.dataset.type==2){
      this.setData({
        yaoUnitIndex:e.currentTarget.dataset.index
      })

    }
  },
  bindChange(e){
    console.log(e)
    
    this.setData({
      curUnit:e.detail.value
    })
  },
  hideModal() {
    this.setData({
      showModal: false,
      // curUnit:[0,0,0]
    })
  },
  submitAddYao(e) {
    
    if(this.data.modalType==2){
      let unitValue=this.data.unitAttr1[this.data.curUnit[0]]+this.data.unitAttr2[this.data.curUnit[1]]+this.data.unitAttr3[this.data.curUnit[2]]
      this.data.yaoAttr[this.data.yaoUnitIndex].unit=unitValue
      this.setData({
        yaoAttr: this.data.yaoAttr
      })
      this.hideModal()
      return
    }
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/baby/addMedicines',
      data: {
        babyId: "32",
        medicines: e.detail.value.yaoInput
      },
      method: 'post',
      success: (res) => {
        console.log(res, 122)
        if (res.data.code == 200) {
          this.getBaby()
          this.hideModal()
        }
      }
    })
  },
  getBaby() {


    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/baby/view',
      data: {
        id: 32
      },
      method: 'get',
      success: (res) => {
        console.log(res, 122)
        if (res.data.code == 200) {
          if (res.data.data.medicines) {

            this.setData({
              medicines: (res.data.data.medicines).split(','),
            })
          }
        }
      }
    })
  },
  select(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.currentTarget.dataset.index
    })
  },
  onTextChange(e) {
    this.data.yaoAttr[e.currentTarget.dataset.index].value = e.detail.value
    this.setData({
      yaoAttr: this.data.yaoAttr
    })
  },
  delYao(e) {
    console.log(this.data.yaoAttr[e.currentTarget.dataset.index])
    this.data.yaoAttr.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      yaoAttr: this.data.yaoAttr
    })
  },
  addYao(e) {
    let item = {
      value: e.currentTarget.dataset.val || '',
      unit: ''
    }
    this.data.yaoAttr.push(item)
    this.setData({
      yaoAttr: this.data.yaoAttr
    })
  },
  pickerChangeType(e) {
    console.log(e, 21)
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  saveYao(){
    let addAttr=JSON.parse(JSON.stringify(this.data.yaoAttr))
    for(let i in this.data.yaoAttr){
      this.data.medicines.some(item=>{
        if(this.data.yaoAttr[i].value==item){
          addAttr.splice(i,1)
        }
      })
    }
    addAttr.some(item=>{
      
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/baby/addMedicines',
      data: {
        babyId: "32",
        medicines: item.value
      },
      method: 'post',
      success: (res) => {
        console.log(res, 122)
        if (res.data.code == 200) {}
      }
    })
    })
  },
  formSubmit(e) {
    console.log(e, 12122)
    if(this.data.yaoAttr.length<=0){
      wx.showToast({
        title: '请添加药品/营养品',
        icon: 'none',
        duration: 1500 //持续的时间
      })
      return
    }
    let hasNone=false
    this.data.yaoAttr.some(item=>{
      if(!item.value || !item.unit){
        hasNone=true
      }
    })
    if(hasNone){
      wx.showToast({
        title: '请填写药品/营养品',
        icon: 'none',
        duration: 1500 //持续的时间
      })
      return

    }
    // let useEdication=[]
    // this.data.yaoAttr.some(item=>{
    //   let i=item.value+item.unit
    //   useEdication.push(i)
    // })
    let post = {
      babyId: 32,
      typeId: this.data.curCate,

    }
    post.detail = {
      recordDate: this.data.recordDate,
      temperature: e.detail.value.temperature,
      useEdication:JSON.stringify(this.data.yaoAttr),
      description: e.detail.value.description,
      picture: String(this.data.imageAttr)
    }
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/add',
      data: post,
      method: 'post',
      success: (res) => {
        console.log(res, 122)
        if (res.data.code == 200) {

          this.saveYao()
          wx.showToast({
            title: '保存成功！',
            icon: 'none',
            duration: 1500 //持续的时间
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
  },
  changeCate(e) {
    this.setData({
      curCate: e.currentTarget.dataset.index
    })
  },
  delImg(e) {
    this.data.imageAttr.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      imageAttr: this.data.imageAttr
    })
  },
  addImg() {
    wx.chooseImage({
      count: 9,
      success: (chooseImageRes) => {
        console.log(chooseImageRes)
        const tempFilePaths = chooseImageRes.tempFilePaths;
        tempFilePaths.some((item) => {
          wx.uploadFile({
            url: 'https://babyplanet.isv-dev.youzan.com/common/image/upload', //仅为示例，非真实的接口地址
            filePath: item,
            fileType: 'image',
            name: 'file',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'multipart/form-data',
            },
            success: (uploadFileRes) => {
              let res = JSON.parse(uploadFileRes.data)
              if (res.code == 200) {
                this.data.imageAttr.push(res.data.imageUrl)
                this.setData({
                  imageAttr: this.data.imageAttr
                })
              }
            },
          });
        })
      }
    });
  },
  initData() {

    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/selectConfig/view',
      data: {
        code: 'yp_unit'
      },
      method: 'get',
      success: (res) => {
        if (res.data.code == 200) {
          let attr = []
          res.data.data.opts.some(item => {
            attr.push(item.name)
          })
          this.setData({
            unitAttr3: attr
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  onPickerChange(e){
    console.log(e,12121)
    this.setData({
      recordDate:e.detail.dateString
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    let now = new Date();
    let year = now.getFullYear();	// 得到年份
    let month = now.getMonth()+1;	// 得到月份
    let date = now.getDate();	// 得到日期
    let hour = now.getHours();	// 得到小时
    let min = Number(now.getMinutes())<=9?('0'+now.getMinutes()):now.getMinutes();	// 得到分钟
    let curTimeAll = year + "-" + month + "-" + date +" " + hour + ":"+ min
    this.setData({
      recordDate:curTimeAll
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})